//
//  NSDictionary+MCToolbox.m
//  MCToolbox
//
//  Created by J. G. Pusey on 10/25/10.
//  Copyright 2010 MedCommons, Inc. All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are
//  met:
//
//  * Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of MedCommons, Inc. nor the names of its contributors
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
//  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
//  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
//  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
//  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#import "NSDictionary+MCToolbox.h"
#import "NSString+MCToolbox.h"

@implementation NSDictionary (MCToolbox)

- (NSArray *) arrayForKey: (id) key
{
    id obj = [self objectForKey: key];

    if (obj && [obj isKindOfClass: [NSArray class]])
        return obj;

    return nil;
}

- (BOOL) boolForKey: (id) key
{
    id obj = [self objectForKey: key];

    if (obj)
    {
        if ([obj isKindOfClass: [NSNumber class]])
            return [(NSNumber *) obj boolValue];

        if ([obj isKindOfClass: [NSString class]])
        {
            NSString *str = [(NSString *) obj stringByTrimmingWhitespace];

            if ([str length] > 0)
                return [str boolValue];
        }
    }

    return NO;
}

- (NSData *) dataForKey: (id) key
{
    id obj = [self objectForKey: key];

    if (obj && [obj isKindOfClass: [NSData class]])
        return obj;

    return nil;
}

- (NSDate *) dateForKey: (id) key   // unixDateForKey ???
{
    id obj = [self objectForKey: key];

    if (obj)
    {
        if ([obj isKindOfClass: [NSNumber class]])
            return [NSDate dateWithTimeIntervalSince1970: [(NSNumber *) obj doubleValue]];

        if ([obj isKindOfClass: [NSString class]])
        {
            NSString *str = [(NSString *) obj stringByTrimmingWhitespace];

            if ([str length] > 0)
                return [NSDate dateWithTimeIntervalSince1970: [str doubleValue]];
        }
    }

    return nil;
}

- (NSDictionary *) dictionaryForKey: (id) key
{
    id obj = [self objectForKey: key];

    if (obj && [obj isKindOfClass: [NSDictionary class]])
        return obj;

    return nil;
}

- (double) doubleForKey: (id) key
{
    id obj = [self objectForKey: key];

    if (obj)
    {
        if ([obj isKindOfClass: [NSNumber class]])
            return [(NSNumber *) obj doubleValue];

        if ([obj isKindOfClass: [NSString class]])
        {
            NSString *str = [(NSString *) obj stringByTrimmingWhitespace];

            if ([str length] > 0)
                return [str doubleValue];
        }
    }

    return 0.0;
}

- (float) floatForKey: (id) key
{
    id obj = [self objectForKey: key];

    if (obj)
    {
        if ([obj isKindOfClass: [NSNumber class]])
            return [(NSNumber *) obj floatValue];

        if ([obj isKindOfClass: [NSString class]])
        {
            NSString *str = [(NSString *) obj stringByTrimmingWhitespace];

            if ([str length] > 0)
                return [str floatValue];
        }
    }

    return 0.0f;
}

- (NSInteger) integerForKey: (id) key
{
    id obj = [self objectForKey: key];

    if (obj)
    {
        if ([obj isKindOfClass: [NSNumber class]])
            return [(NSNumber *) obj integerValue];

        if ([obj isKindOfClass: [NSString class]])
        {
            NSString *str = [(NSString *) obj stringByTrimmingWhitespace];

            if ([str length] > 0)
                return [str integerValue];
        }
    }

    return 0;
}

- (NSMutableArray *) mutableArrayForKey: (id) key
{
    id obj = [self objectForKey: key];

    if (obj && [obj isKindOfClass: [NSMutableArray class]])
        return obj;

    return nil;
}

- (NSMutableDictionary *) mutableDictionaryForKey: (id) key
{
    id obj = [self objectForKey: key];

    if (obj && [obj isKindOfClass: [NSMutableDictionary class]])
        return obj;

    return nil;
}

- (NSArray *) stringArrayForKey: (id) key
{
    NSArray *tmpArray = [self arrayForKey: key];

    if (tmpArray)
    {
        for (id obj in tmpArray)
        {
            if (![obj isKindOfClass: [NSString class]])
                return nil;
        }

        return tmpArray;
    }

    return nil;
}

- (NSString *) stringForKey: (id) key
{
    return [self stringForKey: key
               trimWhitespace: YES
                   allowEmpty: NO];
}

- (NSString *) stringForKey: (id) key
             trimWhitespace: (BOOL) trimWhitespace
                 allowEmpty: (BOOL) allowEmpty
{
    id obj = [self objectForKey: key];

    if (obj && [obj isKindOfClass: [NSString class]])
    {
        NSString *str = (trimWhitespace ?
                         [(NSString *) obj stringByTrimmingWhitespace] :
                         (NSString *) obj);

        if (allowEmpty || ([str length] > 0))
            return str;
    }

    return nil;
}

- (NSUInteger) unsignedIntegerForKey: (id) key
{
    id obj = [self objectForKey: key];

    if (obj)
    {
        if ([obj isKindOfClass: [NSNumber class]])
            return [(NSNumber *) obj unsignedIntegerValue];

        if ([obj isKindOfClass: [NSString class]])
        {
            NSString *str = [(NSString *) obj stringByTrimmingWhitespace];

            if ([str length] > 0)
                return (NSUInteger) [str integerValue];
        }
    }

    return 0;
}

- (NSURL *) URLForKey: (id) key
{
    NSString *str = [self stringForKey: key];

    return (str ?
            [NSURL URLWithString: str] :
            nil);
}

@end
